package me.zk.controller;

import me.zk.annotation.Limit;
import me.zk.annotation.RedisLimit;
import me.zk.dto.DateVo;
import me.zk.service.DemoService;
import me.zk.service.HService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Author: zhang kai
 * @Date: 2022/08/09
 * @Description:
 */

@RestController
public class DateController {

    @Value("${author.name:zk}")
    public String authorName;

    @Autowired
    private HService hService;
    @Autowired
    private DemoService demoServiceImpl1;

    @GetMapping("/user/{id}")
    @Limit(key = "cachingTest", permitsPerSecond = 1,timeout = 500)
    public int getMsg(@PathVariable("id")int id) {
        return hService.getUserId(id);
    }


    @GetMapping("/author")
    @RedisLimit(key = "cachingTest", count = 2, period = 2, msg = "当前排队人数较多，请稍后再试！")
    public String getAuthor() {
        return authorName;
    }
    /**
     * 通过表单获取传递的数据
     * @param vo
     * @return
     */
    @PostMapping("/post_form_date")
    public DateVo postFormDate(DateVo vo) {
        System.out.println("date:" + vo.getDate());
        return vo;
    }

    /**
     * http body 中传递json格式数据
     * @param vo
     * @return
     */
    @PostMapping("/post_date")
    public DateVo postDate(@RequestBody DateVo vo) {
        System.out.println("date:" + vo.getDate());
        return vo;
    }


    public static String unicodeToStr(String unicode) {
        StringBuilder sb = new StringBuilder();
        String[] hex = unicode.split("\\\\u");
        for (int i = 1; i < hex.length; i++) {
            int index = Integer.parseInt(hex[i], 16);
            sb.append((char) index);
        }
        return sb.toString();
    }
}
